package edu.stanford.prpl.impl;

import java.util.Iterator;

import edu.stanford.prpl.api.Group;
import edu.stanford.prpl.api.Identity;
import edu.stanford.prpl.api.PRPLObjectManager;
import edu.stanford.prpl.api.Resource;
import edu.stanford.prpl.api.PRPLObjectManager.GroupManager;

public class GroupImpl extends PRPLObjectImpl implements Group {

	GroupManager groupManager;
	
	String groupName;

	public GroupImpl(String groupUri, PRPLObjectManager objManager) {
		super(groupUri, objManager);
		this.groupManager = objManager.getGroupManager();
	}

	//@Override
	public void addMember(Identity identity) {
		this.groupManager.addIdentityToGroup(objectURI, identity.getURI());
	}

	//@Override
	public Identity[] getMembers() {
		return this.groupManager.getAllGroupMembers(objectURI);
	}

	//@Override
	public String getName() {
		if (this.groupName == null)
			this.groupName = this.groupManager.getGroupName(objectURI);
		return this.groupName;
	}

	//@Override
	public Iterator<Resource> getResources() {
		return prplObjectManager.getResourceManager().getAllResourcesSharedWithGroup(objectURI).iterator();
	}

	//@Override
	public String getURI() {
		return this.objectURI;
	}

	//@Override
	public boolean isMember(Identity identity) {
		return this.groupManager.isIdentityGroupMember(objectURI, identity.getURI());
	}

	//@Override
	public void removeMember(Identity identity) {
		this.groupManager.removeIdentityFromGroup(objectURI, identity.getURI());
	}

	//@Override
	public void share(Resource resource) {
		this.prplObjectManager.getResourceManager().shareResourceWithGroups(resource.getURI(), objectURI);
	}

	//@Override
	public void unshare(Resource resource) {
		this.prplObjectManager.getResourceManager().unshareResourceWithGroups(resource.getURI(), objectURI);
	}

}
